package com.zhaoqi.hr.entity;

import javax.persistence.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 结算记录实体
 */
@Entity
@Table(name = "settlements")
@EntityListeners(AuditingEntityListener.class)
public class Settlement {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(unique = true, nullable = false, length = 50)
    private String settlementNo; // 结算单号
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "contract_id", nullable = false)
    private WorkContract contract;
    
    @Column(nullable = false)
    private LocalDateTime settlementPeriodStart; // 结算周期开始
    
    @Column(nullable = false)
    private LocalDateTime settlementPeriodEnd; // 结算周期结束
    
    @Column(precision = 8, scale = 2)
    private BigDecimal totalWage; // 总工资
    
    @Column(precision = 8, scale = 2)
    private BigDecimal socialInsurance; // 社保费用
    
    @Column(precision = 8, scale = 2)
    private BigDecimal platformFee; // 平台服务费
    
    @Column(precision = 8, scale = 2)
    private BigDecimal actualPayout; // 实际支付金额
    
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private SettlementStatus status = SettlementStatus.PENDING;
    
    @Column(length = 100)
    private String paymentMethod; // 支付方式
    
    @Column(length = 100)
    private String paymentAccount; // 支付账户
    
    @Column
    private LocalDateTime paidAt; // 支付时间
    
    @Column(columnDefinition = "TEXT")
    private String remark; // 备注
    
    @CreatedDate
    @Column(nullable = false, updatable = false)
    private LocalDateTime createdAt;
    
    @LastModifiedDate
    @Column(nullable = false)
    private LocalDateTime updatedAt;
    
    /**
     * 结算状态枚举
     */
    public enum SettlementStatus {
        PENDING("待结算"),
        PROCESSING("结算中"),
        COMPLETED("已完成"),
        FAILED("结算失败");
        
        private final String description;
        
        SettlementStatus(String description) {
            this.description = description;
        }
        
        public String getDescription() {
            return description;
        }
    }

    // Getter and Setter methods
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getSettlementNo() {
        return settlementNo;
    }

    public void setSettlementNo(String settlementNo) {
        this.settlementNo = settlementNo;
    }

    public WorkContract getContract() {
        return contract;
    }

    public void setContract(WorkContract contract) {
        this.contract = contract;
    }

    public LocalDateTime getSettlementPeriodStart() {
        return settlementPeriodStart;
    }

    public void setSettlementPeriodStart(LocalDateTime settlementPeriodStart) {
        this.settlementPeriodStart = settlementPeriodStart;
    }

    public LocalDateTime getSettlementPeriodEnd() {
        return settlementPeriodEnd;
    }

    public void setSettlementPeriodEnd(LocalDateTime settlementPeriodEnd) {
        this.settlementPeriodEnd = settlementPeriodEnd;
    }

    public BigDecimal getTotalWage() {
        return totalWage;
    }

    public void setTotalWage(BigDecimal totalWage) {
        this.totalWage = totalWage;
    }

    public BigDecimal getSocialInsurance() {
        return socialInsurance;
    }

    public void setSocialInsurance(BigDecimal socialInsurance) {
        this.socialInsurance = socialInsurance;
    }

    public BigDecimal getPlatformFee() {
        return platformFee;
    }

    public void setPlatformFee(BigDecimal platformFee) {
        this.platformFee = platformFee;
    }

    public BigDecimal getActualPayout() {
        return actualPayout;
    }

    public void setActualPayout(BigDecimal actualPayout) {
        this.actualPayout = actualPayout;
    }

    public SettlementStatus getStatus() {
        return status;
    }

    public void setStatus(SettlementStatus status) {
        this.status = status;
    }

    public String getPaymentMethod() {
        return paymentMethod;
    }

    public void setPaymentMethod(String paymentMethod) {
        this.paymentMethod = paymentMethod;
    }

    public String getPaymentAccount() {
        return paymentAccount;
    }

    public void setPaymentAccount(String paymentAccount) {
        this.paymentAccount = paymentAccount;
    }

    public LocalDateTime getPaidAt() {
        return paidAt;
    }

    public void setPaidAt(LocalDateTime paidAt) {
        this.paidAt = paidAt;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Settlement that = (Settlement) o;
        return Objects.equals(id, that.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public String toString() {
        return "Settlement{" +
                "id=" + id +
                ", settlementNo='" + settlementNo + '\'' +
                ", settlementPeriodStart=" + settlementPeriodStart +
                ", settlementPeriodEnd=" + settlementPeriodEnd +
                ", totalWage=" + totalWage +
                ", socialInsurance=" + socialInsurance +
                ", platformFee=" + platformFee +
                ", actualPayout=" + actualPayout +
                ", status=" + status +
                ", paymentMethod='" + paymentMethod + '\'' +
                ", paymentAccount='" + paymentAccount + '\'' +
                ", paidAt=" + paidAt +
                ", createdAt=" + createdAt +
                ", updatedAt=" + updatedAt +
                '}';
    }
}
